/**
 * @file geminai_para.c
 * @brief 参数保存到nvs
 * @author zl
 */

#include "geminai_para.h"

uint16_t alarm_stat;//报警标志位
uint16_t overtemp_time;//超温报警时间
uint8_t led_status = 1;//0-关闭 1-开启
uint8_t frig_sys_switch = 1;//系统开关状态
unsigned long attribute_send_event = 0x5ff;//开机默认全发送
frig_ctl_para temp_ctl_para;
frig_temp sys_temp;
main_dev_monitor devices_tim_cnt;//主要器件工作时间及次数统计
relay_state relay_ctrl;
base_station_t base_station;
system_para_t system_para;

void initialize_NVS(void)
{
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
      ESP_ERROR_CHECK(nvs_flash_erase());
      ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);
}

void Nvs_write_blob_to_flash(const char* nameSpace, const char* key, const void* value, int length)
{
    nvs_handle handle;
    esp_err_t ret;

    ret = nvs_open(nameSpace, NVS_READWRITE, &handle);
    if (ret != ESP_OK) {
        return;
    }

    ret = nvs_set_blob(handle, key, value, length);
    if (ret != ESP_OK) {
        nvs_close(handle);
        return;
    }

    ret = nvs_commit(handle);
    if (ret != ESP_OK) {
        nvs_close(handle);
        return;
    }

    nvs_close(handle);
}

int Nvs_read_blob_to_flash(const char* nameSpace, const char* key, void* value, int length)
{
    size_t len = length;
    nvs_handle handle;
    esp_err_t ret = nvs_open(nameSpace, NVS_READWRITE, &handle);
    if (ret != ESP_OK) {
        return -1;
    }

    ret = nvs_get_blob(handle, key, value, &len);
    nvs_close(handle);

    return (ret == ESP_OK) ? 0 : -1;
}

void set_default_para(void)
{
    temp_ctl_para.temp_des                      = 3;    //目标温度3度
    temp_ctl_para.temp_diff                     = 3;    //回差温度3度
    temp_ctl_para.delay_tim                     = 5;    //最小停机时间5min
    temp_ctl_para.compressor_start_delay        = 5;    //压缩机延时启动时间5S
    temp_ctl_para.device_lock                   = 0;    //锁机
    temp_ctl_para.forced_cooling                = 0;    //强制制冷
    temp_ctl_para.defrost_time                  = 20;   //除霜时间20min
    temp_ctl_para.overtemp                      = 30;   //高温报警温度30℃
    temp_ctl_para.lowtemp                       = -5;   //低温报警温度-5℃
    temp_ctl_para.overtemp_flag                 = 0;    //高温报警标志
    temp_ctl_para.lowtemp_flag                  = 0;    //低温报警标志
    temp_ctl_para.overtemp_fans_protect_temp    = 20;   //高温风机保护温度20℃
    temp_ctl_para.defrost_shielding_time        = 10;   //除霜后高温报警屏蔽时间10min
    temp_ctl_para.overtemp_alarm_delay_time     = 60;   //高温报警延时时间1h
    temp_ctl_para.led_pwm                       = 100;  //led亮度默认100%
    temp_ctl_para.start_shielding_time          = 1440; //上电高温报警屏蔽时间24h
    temp_ctl_para.defrost_cycle                 = 240;  //除霜周期4h
    temp_ctl_para.overtemp_fans_protect_time    = 2880; //高温风机保护时间48h
    temp_ctl_para.temp_offset                   = 15;  //温度传感器偏移温度1.5℃
    temp_ctl_para.temp_des_min                  = -50;  //目标温度最小值-50℃
    temp_ctl_para.temp_des_max                  = 50;   //目标温度最大值50℃
    temp_ctl_para.eco_state_temp                = 5;    //节能模式上调温度5℃
    temp_ctl_para.sleep_state_temp              = 12;    //休眠模式上调温度12℃
    temp_ctl_para.comp_max_runtime              = 0;  //压缩机最大运行时间（不限制）
    temp_ctl_para.comp_fault_runtime             = 10;   //传感器故障运行时间10min
    temp_ctl_para.comp_fault_dowmtime            = 15;   //传感器异常停机时间15min
    temp_ctl_para.forced_cold_time               = 2;    //强制制冷时间2h
    temp_ctl_para.defrost_time_type              = 1;    //除霜计时方式(0:压缩机累计运行时间 1:控制器运行时间)
    temp_ctl_para.defrost_boot_shielding_time    = 20;    //上电除霜屏蔽时间20h
    temp_ctl_para.defrost_show_type              = 2;    //除霜显示方式(0:温度与df交替显示 1:显示除霜前的温度 2:显示df)
    temp_ctl_para.sound_output                   = 1;    //声音输出(0:不输出 1:输出)
    temp_ctl_para.door_open_type                 = 1;    //门打开时关闭风机，门关闭时开启风机
    temp_ctl_para.door_alarm_time                = 5;    //开门报警时间5min
    temp_ctl_para.light_ctrl                     = 1;    //灯光控制(0:屏蔽控制 1:控制)
    temp_ctl_para.fan_run_type                   = 0;    //风机运行方式(0:常转 1:随压缩机启停)
    temp_ctl_para.defrost_fan_run_type           = 0;    //除霜时风机运行方式(0:除霜时风机运行 1:除霜时风机停止)
    temp_ctl_para.door_open_fan_time             = 5;    //关门风机启动时间5s
    temp_ctl_para.open_door_times_cancel_defrost = 5;    //取消上电除霜屏蔽时间的开门次数
    temp_ctl_para.frig_run_mode                  = 0;    //运行模式(0:正常模式 1:节能模式 2:休眠模式)
}

void initialize_para(void)
{
    if(Nvs_read_blob_to_flash(NS_FRIG_PARA, KEY_FRIG_PARA, &temp_ctl_para, sizeof(frig_ctl_para)))
    {
        //读取数据失败，设置为默认值，进行保存
        set_default_para();
        Nvs_write_blob_to_flash(NS_FRIG_PARA, KEY_FRIG_PARA, &temp_ctl_para, sizeof(frig_ctl_para));
    }
    //读取成功，对数据进行校验
    if(temp_ctl_para.temp_des > 20 || temp_ctl_para.temp_des < -5){temp_ctl_para.temp_des = 3;}
    if(temp_ctl_para.temp_diff > 10){temp_ctl_para.temp_diff = 3;}
    if(temp_ctl_para.delay_tim > 10){temp_ctl_para.delay_tim = 5;}
    // if(temp_ctl_para.compressor_start_delay > 10){temp_ctl_para.compressor_start_delay = 2;}
    if(temp_ctl_para.device_lock > 1){temp_ctl_para.device_lock = 0;}
    if(temp_ctl_para.forced_cooling > 1){temp_ctl_para.forced_cooling = 0;}
    if(temp_ctl_para.defrost_time == 0 || temp_ctl_para.defrost_time > 90){temp_ctl_para.defrost_time = 10;}
    if(temp_ctl_para.overtemp < temp_ctl_para.lowtemp || temp_ctl_para.overtemp > 70){temp_ctl_para.overtemp = 30;}
    if(temp_ctl_para.lowtemp > temp_ctl_para.overtemp || temp_ctl_para.lowtemp < -40){temp_ctl_para.lowtemp = -5;}
    if(temp_ctl_para.overtemp_flag > 1){temp_ctl_para.overtemp_flag = 0;}
    if(temp_ctl_para.lowtemp_flag > 1){temp_ctl_para.lowtemp_flag = 0;}
    if(temp_ctl_para.overtemp_fans_protect_temp > 30){temp_ctl_para.overtemp_fans_protect_temp = 20;}
    if(temp_ctl_para.defrost_shielding_time > 90){temp_ctl_para.defrost_shielding_time = 10;}
    if(temp_ctl_para.overtemp_alarm_delay_time > 180){temp_ctl_para.overtemp_alarm_delay_time = 60;}
    if(temp_ctl_para.led_pwm > 100){temp_ctl_para.led_pwm = 100;}
    if(temp_ctl_para.start_shielding_time > 3600 || temp_ctl_para.start_shielding_time == 0){temp_ctl_para.start_shielding_time = 1440;}
    if(temp_ctl_para.defrost_cycle > 3600){temp_ctl_para.defrost_cycle = 240;}
    if(temp_ctl_para.overtemp_fans_protect_time > 2880||temp_ctl_para.overtemp_fans_protect_time == 0){temp_ctl_para.overtemp_fans_protect_time = 2880;}
    if(temp_ctl_para.temp_offset > 100 || temp_ctl_para.temp_offset < -100){temp_ctl_para.temp_offset = 15;}
    if(temp_ctl_para.temp_des_min > temp_ctl_para.temp_des_max || temp_ctl_para.temp_des_min < -50){temp_ctl_para.temp_des_min = -50;}
    if(temp_ctl_para.temp_des_max > 50 || temp_ctl_para.temp_des_max < -50){temp_ctl_para.temp_des_max = 10;}
    if(temp_ctl_para.eco_state_temp > 10 ){temp_ctl_para.eco_state_temp = 5;}
    if(temp_ctl_para.sleep_state_temp > 20 || temp_ctl_para.sleep_state_temp < 5){temp_ctl_para.sleep_state_temp = 12;}
    if(temp_ctl_para.comp_fault_runtime > 60){temp_ctl_para.comp_fault_runtime = 10;}
    if(temp_ctl_para.comp_fault_dowmtime > 60 ||temp_ctl_para.comp_fault_dowmtime < 1){temp_ctl_para.comp_fault_dowmtime = 15;}
    if(temp_ctl_para.forced_cold_time > 10){temp_ctl_para.forced_cold_time = 2;}
    if(temp_ctl_para.defrost_time_type > 1){temp_ctl_para.defrost_time_type = 1;}
    if(temp_ctl_para.defrost_boot_shielding_time > 90){temp_ctl_para.defrost_boot_shielding_time = 20;}
    if(temp_ctl_para.defrost_show_type > 2){temp_ctl_para.defrost_show_type = 2;}
    if(temp_ctl_para.sound_output > 1){temp_ctl_para.sound_output = 1;}
    if(temp_ctl_para.door_open_type > 3){temp_ctl_para.door_open_type = 1;}
    if(temp_ctl_para.door_alarm_time > 30){temp_ctl_para.door_alarm_time = 5;}
    if(temp_ctl_para.light_ctrl > 1){temp_ctl_para.light_ctrl = 1;}
    if(temp_ctl_para.fan_run_type > 2){temp_ctl_para.fan_run_type = 0;}
    if(temp_ctl_para.defrost_fan_run_type > 1){temp_ctl_para.defrost_fan_run_type = 0;}
    if(temp_ctl_para.door_open_fan_time > 10){temp_ctl_para.door_open_fan_time = 5;}
    if(temp_ctl_para.open_door_times_cancel_defrost > 10){temp_ctl_para.open_door_times_cancel_defrost = 5;}
    if(temp_ctl_para.frig_run_mode > 2){temp_ctl_para.frig_run_mode = 0;}
    
    if(Nvs_read_blob_to_flash(NS_FRIG_PARA, KEY_SYSTEM_PARA, &system_para, sizeof(system_para)))
    {
        //读取数据失败，设置为默认值，进行保存
        system_para.report_interval = 1800;
        system_para.volume = 100;
        Nvs_write_blob_to_flash(NS_FRIG_PARA, KEY_SYSTEM_PARA, &system_para, sizeof(system_para));
    }
    //数据校验
    if(system_para.report_interval < 60 ){system_para.report_interval = 1800;}
    if(system_para.volume > 100){system_para.volume = 100;}    

    ESP_ERROR_CHECK(esp_read_mac(base_station.bt_mac, ESP_MAC_WIFI_STA));
    ESP_ERROR_CHECK(esp_read_mac(base_station.wifi_mac, ESP_MAC_BT));
    GEMINAI_LOGI("WIFI_STA MAC", "%x:%x:%x:%x:%x:%x",
             base_station.bt_mac[0], base_station.bt_mac[1], base_station.bt_mac[2],
             base_station.bt_mac[3], base_station.bt_mac[4], base_station.bt_mac[5]);
    GEMINAI_LOGI("WIFI_STA MAC", "%x:%x:%x:%x:%x:%x",
             base_station.wifi_mac[0], base_station.wifi_mac[1], base_station.wifi_mac[2],
             base_station.wifi_mac[3], base_station.wifi_mac[4], base_station.wifi_mac[5]);       
    overtemp_time = temp_ctl_para.start_shielding_time;
}

void save_frig_para(void)
{
    Nvs_write_blob_to_flash(NS_FRIG_PARA, KEY_FRIG_PARA, &temp_ctl_para, sizeof(frig_ctl_para));
    Nvs_write_blob_to_flash(NS_FRIG_PARA, KEY_SYSTEM_PARA, &system_para, sizeof(system_para));
}

int timer_wakeup_count_get(void)
{
    nvs_handle handle;
    esp_err_t ret;

    ret = nvs_open(NS_FRIG_PARA, NVS_READWRITE, &handle);
    if (ret != ESP_OK) {
        return 0;
    }
    int32_t timerWakeupCount = 0;
    ret = nvs_get_i32(handle, KEY_TIMER_WAKEUP_COUNT, &timerWakeupCount);
    if(ret != ESP_OK){
        nvs_set_i32(handle, KEY_TIMER_WAKEUP_COUNT, 0);
    }
    nvs_commit(handle);
    nvs_close(handle);
    
    return (ret == ESP_OK) ? timerWakeupCount : 0;
}

void timer_wakeup_count_set(int count)
{
    nvs_handle handle;
    esp_err_t ret;

    ret = nvs_open(NS_FRIG_PARA, NVS_READWRITE, &handle);
    if (ret != ESP_OK) {
        return;
    }
    ret = nvs_set_i32(handle, KEY_TIMER_WAKEUP_COUNT, count);
    nvs_commit(handle);
    nvs_close(handle);
    
    return;
}

